from sqlalchemy import Column, Integer, String, Float, Boolean, DateTime, ForeignKey, JSON
from sqlalchemy.orm import relationship
from datetime import datetime
from database import Base

class DroneType(Base):
    __tablename__ = "drone_types"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), unique=True, index=True, nullable=False)
    description = Column(String(255))
    max_speed = Column(Float)
    max_payload = Column(Float)
    max_flight_time = Column(Integer)  # 分钟
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    
    # 关系
    drones = relationship("Drone", back_populates="drone_type")

class Drone(Base):
    __tablename__ = "drones"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), unique=True, index=True, nullable=False)
    serial_number = Column(String(100), unique=True, index=True)
    drone_type_id = Column(Integer, ForeignKey('drone_types.id'))
    operator_id = Column(Integer, ForeignKey('users.id'))
    department_id = Column(Integer, ForeignKey('departments.id'))
    status = Column(String(20), default="available")  # available, busy, maintenance, broken
    latitude = Column(Float)  # 当前位置
    longitude = Column(Float)  # 当前位置
    altitude = Column(Float)   # 当前高度
    battery_level = Column(Integer)  # 电池电量百分比
    last_flight_time = Column(DateTime)  # 最后飞行时间
    total_flight_time = Column(Integer)  # 总飞行时间（分钟）
    maintenance_date = Column(DateTime)  # 下次维护日期
    created_at = Column(DateTime, default=datetime.now)
    updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
    
    # 关系
    drone_type = relationship("DroneType", back_populates="drones")
    operator = relationship("User", back_populates="drones")
    department = relationship("Department", backref="drones")
